////
////  SRClient.m
////  mall
////
////  Created by 一米阳光 on 15/7/29.
////  Copyright (c) 2015年 seekool. All rights reserved.
////
//
//#import "SRClient.h"
//#import "AFHTTPSessionManager.h"
//
//@interface SRClient()
//
//@property (strong,nonatomic)AFHTTPSessionManager *sessionManager;
//
//@end
//
//@implementation SRClient
//
//
//+ (instancetype) sharedClient {
//    static id __instance;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        __instance = [[self alloc] init];
//    });
//    return __instance;
//}
//
//- (instancetype) init {
//    self = [super init];
//    if(self){
//        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
//        config.timeoutIntervalForRequest = 20.0;
//        
//        
//        _sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL] sessionConfiguration:config];
//        _sessionManager.securityPolicy.allowInvalidCertificates = YES;
//        _sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
//        
//    }
//    
//    return self;
//}
//
//- (NSURLSessionDataTask *) POST:(NSString *)api parameters:(NSDictionary *)parameters completion:(SRAPIResponseBLock) completion {
//    NSDictionary *signedParams = [self sign:parameters];
//    NSURLSessionDataTask *dataTask =
//    [_sessionManager POST:api parameters:signedParams success:^(NSURLSessionDataTask *task, id responseObject) {
//        if(!completion) return;
//        
//        NSError *newError = nil;
//        if([responseObject isKindOfClass:[NSDictionary class]] && [responseObject objectForKey:@"error_code"]){
//            NSInteger code = [responseObject[@"error_code"] integerValue];
//            newError = [self errorWithCode:code
//                        parsedJSONResponse:responseObject
//                                innerError:nil
//                                   message:responseObject[@"error_msg"]];
//        }
//        completion(task, responseObject, newError);
//    } failure:^(NSURLSessionDataTask *task, NSError *error) {
//        if(!completion) return;
//        NSError *newError = [self errorWithCode:LTErrorCodeNetworkError
//                             parsedJSONResponse:nil
//                                     innerError:error
//                                        message:error.localizedDescription];
//        completion(task, nil, newError);
//    }];
//    return dataTask;
//}
//- (RACSignal *)signalWithAPI:(NSString *)api parameters:(NSDictionary *)parameters;
//{
//    NSString *apiURLString = [api stringByAppendingString:@"?format=json"];
//    NSMutableDictionary *para;
//    if (parameters) {
//        para= [parameters mutableCopy];
//    }else{
//        para = [@{} mutableCopy];
//    }
////    [para setObject:[NSString stringWithFormat:@"%@",API_VERSION] forKey:@"version"];
////    [para setObject:[NSString stringWithFormat:@"%@",API_SYSVERSION] forKey:@"opversion"];
////    [para setObject:[NSString stringWithFormat:@"%@",[LTSingleton shareInstance].deviceString] forKey:@"deviceType"];//获取设备型号
//    NSArray *keys = [para allKeys];
//    NSMutableArray *values = [NSMutableArray array];
//    for (id key in keys) {
//        id value = [para objectForKey:key];
//        if ([value isKindOfClass:[NSString class]]&&[SRUtil isBlankString:value]) {
//            [values addObject:key];
//        }
//    }
//    [para removeObjectsForKeys:values];
//    
//    
//    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
//        NSURLSessionDataTask *dataTask =
//        [self POST:apiURLString
//        parameters:para
//        completion:^(NSURLSessionDataTask *task, id responseObject, NSError *error) {
//            if(error){
//                [subscriber sendError:error];
//            }else{
//                [subscriber sendNext:responseObject];
//                [subscriber sendCompleted];
//            }
//        }];
//        return [RACDisposable disposableWithBlock:^{
//            [dataTask cancel];
//        }];
//    }];
//}
//
//
//- (NSError *)errorWithCode:(NSInteger) code
//        parsedJSONResponse:(id)response
//                innerError:(NSError *) innerError
//                   message:(NSString *) message{
//    NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
//    
//    if(innerError){
//        userInfo[LTErrorInnerErrorKey] = innerError;
//    }
//    
//    if(message){
//        userInfo[NSLocalizedDescriptionKey] = message;
//    }
//    
//    NSError *error = [[NSError alloc]initWithDomain:LTErrorAPIDomain
//                                               code:code
//                                           userInfo:userInfo];
//    return error;
//}
//
//#pragma mark - Sign Parameters
//- (NSDictionary *)sign:(NSDictionary *)params{
//    NSArray *keys = [params allKeys];
//    NSArray *sortedKeys = [keys sortedArrayUsingSelector:@selector(compare:)];
//    NSMutableArray *components = [NSMutableArray arrayWithCapacity:keys.count];
//    
//    [sortedKeys enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop){
//        NSString *str = [NSString stringWithFormat:@"%@",params[key]];
//        [components addObject:[NSString stringWithFormat:@"%@=%@", key, [str urlencode]]];
//    }];
//    
//    NSString *queryString = [components componentsJoinedByString:@"&"];
//    NSString *sign = [[queryString stringByAppendingString:APP_KEY] md5];
//    
//    NSMutableDictionary *signedParams = [params mutableCopy];
//    signedParams[@"appid"] = APP_ID;
//    signedParams[@"sign"] = sign;
//    return signedParams;
//}
//
//@end
